package main

import (
	"bytes"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/axgle/mahonia"
	"github.com/gen2brain/beeep"
	"log"
	"os"
	"os/exec"
	"runtime"
	"syscall"
)

func main() {
	switch runtime.GOOS {
	case "windows":
		os.Setenv("FYNE_FONT", "C:/Windows/fonts/msyh.ttc")
		defer os.Unsetenv("FYNE_FONT")
	case "linux":
		os.Setenv("FYNE_FONT", "/usr/share/fonts/msyh.ttc")
		defer os.Unsetenv("FYNE_FONT")
	case "darwin":
		os.Setenv("FYNE_FONT", "/Library/Fonts/STHeiti-Medium.ttc")
		defer os.Unsetenv("FYNE_FONT")
	}

	application := app.New()

	w := application.NewWindow("Hello")
	log.Println(w.Title())
	w.CenterOnScreen()
	w.Resize(fyne.Size{500, 300})
	bar := widget.NewProgressBar()
	w.SetContent(container.NewVBox(
		widget.NewLabel("Hello Fyne!"),
		bar,
		widget.NewButton("退出", func() {
			log.Println("quit 退出")
			application.Quit()
		}),
	))

	bar.SetValue(0.1)

	enc := mahonia.NewEncoder("utf8")
	err := beeep.Notify("标题", enc.ConvertString("测试测试"), "")
	if err != nil {
		panic(err)
	}

	//w2 := application.NewWindow("Hello2")
	//w2.CenterOnScreen()
	//w2.Resize(fyne.Size{1, 1})
	//w2.Show()
	//w2.Close()

	command()
	w.ShowAndRun()
}

func command() string {
	command := exec.Command("chcp")
	outInfo := bytes.Buffer{}
	command.Stdout = &outInfo
	err := command.Start()
	if err != nil {
		fmt.Println(err.Error())
	}
	if err = command.Wait(); err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println(command.ProcessState.Pid())
		fmt.Println(command.ProcessState.Sys().(syscall.WaitStatus).ExitCode)
		fmt.Println(outInfo.String())
		return outInfo.String()
	}
	return ""
}
